package com.suke.czx.modules.tb.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.suke.czx.modules.tb.entity.TbUserAddress;
import com.suke.czx.modules.tb.service.TbUserAddressService;
import com.suke.czx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;


/**
 * 用户收货地址表
 *
 * @author developer
 * @email developer@163.com
 * @date 2025-05-22 13:07:41
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tb/userAddress")
@Api(value = "TbUserAddressController", tags = "用户收货地址表")
public class TbUserAddressController extends AbstractController {
    private final TbUserAddressService tbUserAddressService;

    /**
     * 列表
     */
    @ApiOperation(value = "用户收货地址表列表")
    @PostMapping("/myList")
    public R myList(@RequestBody Map<String, Object> params) {
        //查询列表数据
        String userId = getUserId();
        if (StringUtils.isBlank(userId)) {
            return R.error("参数错误");
        }
        LambdaQueryWrapper<TbUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbUserAddress::getUserId, userId);
        queryWrapper.orderByDesc(TbUserAddress::getIsDefault);
        queryWrapper.eq(TbUserAddress::getIsDeleted, 0);
        final String keyword = mpPageConvert.getKeyword(params);
        return R.ok().setData(tbUserAddressService.list(queryWrapper));
    }


    /**
     * 新增用户收货地址表
     */
    @ApiOperation(value = "新增用户收货地址表数据")
    @SysLog("新增用户收货地址表数据")
    @PostMapping("/save")
    public R save(@RequestBody TbUserAddress tbUserAddress) {
        String userId = getUserId();
        tbUserAddress.setUserId(userId);
        tbUserAddress.setIsDeleted(0);
        tbUserAddress.setCreateDate(new Date());
        LambdaQueryWrapper<TbUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbUserAddress::getUserId, userId);
        queryWrapper.eq(TbUserAddress::getIsDefault, 1);
        long count = tbUserAddressService.count(queryWrapper);
        if(count == 0){
            tbUserAddress.setIsDefault(1);
        }
        if(null == tbUserAddress.getId() || 0L == tbUserAddress.getId()){
            tbUserAddressService.save(tbUserAddress);
        }else{
            tbUserAddressService.updateById(tbUserAddress);
        }
        if(1 == tbUserAddress.getIsDefault()){
            // 把其他的设置为非默认
            LambdaUpdateWrapper<TbUserAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(TbUserAddress::getUserId, userId);
            updateWrapper.eq(TbUserAddress::getIsDefault, 1);
            updateWrapper.set(TbUserAddress::getIsDefault, 0);
            if(null != tbUserAddress.getId() && 0L != tbUserAddress.getId()){
                updateWrapper.ne(TbUserAddress::getId, tbUserAddress.getId());
            }
            tbUserAddressService.update(updateWrapper);
        }
        return R.ok();
    }


    /**
     * 修改
     */
    @ApiOperation(value = "修改用户收货地址表数据")
    @SysLog("修改用户收货地址表数据")
    @PutMapping()
    public R update(@RequestBody TbUserAddress tbUserAddress) {
        tbUserAddressService.updateById(tbUserAddress);
        return R.ok();
    }


    /**
     * 删除
     */
    @ApiOperation(value = "删除用户收货地址表数据")
    @SysLog("删除用户收货地址表数据")
    @PostMapping("/delete")
    public R delete(@RequestBody Map<String,Object> params)  {
        String ids = (String) params.get("ids");
        if(StringUtils.isBlank(ids)){
            return R.error("参数错误");
        }
        String[] addressIds = ids.split(",");
        // 查询数据，判断是否是当前用户的
        LambdaQueryWrapper<TbUserAddress> tbUserAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tbUserAddressLambdaQueryWrapper.in(TbUserAddress::getId, addressIds);
        tbUserAddressLambdaQueryWrapper.eq(TbUserAddress::getUserId, getUserId());
        List<TbUserAddress> list = tbUserAddressService.list(tbUserAddressLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return R.error("参数错误");
        }
        list.forEach(tbUserAddress -> {
            tbUserAddress.setIsDeleted(1);
            tbUserAddress.setUpdateDate(new Date());
        });
        tbUserAddressService.updateBatchById(list);
        return R.ok();
    }

}
